package com.doudoukeji.service.Activity.mypatient;

import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import com.doudoukeji.service.Activity.order.MyOrderAdapter;
import com.doudoukeji.service.Activity.order.OrderListPresenter;
import com.doudoukeji.service.R;
import com.doudoukeji.service.Ui.customlistview.OnRefreshListener;
import com.doudoukeji.service.Ui.customlistview.RefreshListView;
import com.sanhai.android.mvp.EduActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述： 我的患者界面
 * @项目名称：ddgh
 * @类名称： MypatientListActivity
 * @包名称： com.doudoukeji.service.Activity.myatient
 * @创建人：张强 boy
 * @创建时间：2016/3/9 14:06
 * @修改人：
 * @修改时间：2016/3/9 14:06
 * @修改备注： new parent class(用一句话描述修改了什么)
 */

public class MyPatientListActivity extends EduActivity implements MypatientListView {

    // listview
    private RefreshListView mList;
    private MyPatientListAdapter mAdapter;
    private MyPatientListPresenter presenter;
    private List<MyPatientListBean> patientListBeans = new ArrayList<MyPatientListBean>();

    //控件
    private LinearLayout mLlNoPatient;
    private int pageIndex = 1;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_mypatient);
    }

    @Override
    public void init() {
        mLlNoPatient = (LinearLayout) findViewById(R.id.ll_on_patient);

        mList = (RefreshListView) findViewById(R.id.list);
        mList.setHeadAndFoot(true, true);
        mAdapter = new MyPatientListAdapter(this, patientListBeans);
        mList.setAdapter(mAdapter);
        mList.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onScrollListener(AbsListView view,
                                         int firstVisibleItem, int visibleItemCount,
                                         int totalItemCount) {

            }

            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                presenter.getPatientList(pageIndex);

            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                presenter.getPatientList(pageIndex);

            }
        });



        mLlNoPatient.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    @Override
    public void onClickEvent(View view) {

    }

    @Override
    public void getMyPatient(List<MyPatientListBean> patients) {

        if (patients != null && patients.size() > 0) {
            if (pageIndex == 1) {
                patientListBeans.clear();
            }
            patientListBeans.addAll(patients);
            mLlNoPatient.setVisibility(View.GONE);
            mAdapter.notifyDataSetChanged();
        }
        mList.onRefreshFinish();
    }

    @Override
    public void getfail() {
        mAdapter.notifyDataSetChanged();
        mList.onRefreshFinish();
    }

    @Override
    public void getNoData() {

        if (pageIndex == 1) {
            this.patientListBeans.clear();
            mLlNoPatient.setVisibility(View.VISIBLE);
            mList.onRefreshFinish();
        } else {
            mLlNoPatient.setVisibility(View.GONE);
            mList.hideNoMoreData();
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter = new MyPatientListPresenter(this, this);
        presenter.getPatientList(pageIndex);
    }
}
